<?php

class SettingController extends BaseController
{
	/**
	 *查看
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 *创建
	 */
	public function actionCreate()
	{
		$model=new Setting;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Setting']))
		{
			$model->attributes=$_POST['Setting'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->zst_id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * 更新
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Setting']))
		{
			$model->attributes=$_POST['Setting'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->zst_id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 *单条批量删除
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete()
	{
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zst_id in ($id)");

		//执行操作
		if(Setting::model()->deleteAll($criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}

	/**
	 * Lists all models.
	 */
	public function actionUpdateSiteType()
	{
		$model = Setting::model()->findByAttributes(array('zkl_variable'=>'site_shop_type'));
		if(empty($model)||!isset($_GET['type']))
			throw new CHttpException(404,'The requested page does not exist.');
		$model->zkl_valued = (int)$_GET['type']; 
		//执行操作
		if($model->save()){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}	
	}

	/**
	 *配置管理
	 */
	public function actionAdmin()
	{
		$model=new Setting('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Setting']))
			$model->attributes=$_GET['Setting'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * WEB基本设置
	 */
	public function actionWebConfig()
	{
		if(isset($_POST['Setting']))
		{
			$data = $_POST['Setting'];
			foreach ($data as $key => $value) {				
				$model=$this->loadModel($key);				
			    $model->attributes=$value;
			    //print_r($model);
				if($model->save()){
					echo '';
				}
					
			}
			$this->render('webconfig');
			exit;
			
		}
		$this->render('webconfig');
	}


	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Setting::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='setting-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	/**
	 * 清空缓存
	 */
	public function actionDestoryCache(){
		//删除所有缓存文件
		$cacheDir = Yii::app()->filecache->flush();
		//成功跳转
		$this->render('../index/success',array('message'=>'恭喜，清空缓存成功！'));
		exit;
	}
}
